package com.example.fitapp.activity.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.fitapp.activity.R;
import com.example.fitapp.activity.utils.L;

public class EditInputActivity extends AppCompatActivity {
    private EditText editInput;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_input);
        editInput = findViewById(R.id.edit_data);
        editInput.setFocusable(true);
        Intent intent = getIntent();
        String data = intent.getStringExtra("extra_title");
        String value = intent.getStringExtra("extra_value");
        if(value!=null){
            editInput.setText(value);
        }
        setTitle(data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.edit_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_confirm:
                editSubmit();
                break;
            default:
                break;
        }
        return true;
    }
    private void editSubmit(){
        String value = editInput.getText().toString();
        if(value.length()<1){
            Toast.makeText(MyApplication.getContext(),"不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("data_return",value);
        setResult(RESULT_OK,intent);
        finish();
    }
}
